/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.dataset;

import java.util.Collection;

/**
 * DataSet loader for data stored in memory <br>
 * 
 * @author Laurent GUERIN 
 *  
 */
public class DataSetMemoryLoader 
{
	//-----------------------------------------------------------------------------
    //--- CONSTRUCTORS
    //-----------------------------------------------------------------------------
    /**
     * Constructor
     */
    public DataSetMemoryLoader()
    {
        super();
    }

    //-----------------------------------------------------------------------------
    //--- PUBLIC METHODS
    //-----------------------------------------------------------------------------
    public DataSet load( Collection<Object[]> collection )
    {
    	DataSet dataset = new DataSet();
    	load(dataset, collection);
    	return dataset ;
    }
    
    /**
     * Loads the values of the given collection in the given DataSet 
     * @param dataset - an exiting DataSet where the rows will be added
     * @param collection
     * @return the number of rows in the DataSet
     */
    public int load(DataSet dataset, Collection<Object[]> collection)
    {
        if ( collection == null )
        {
            throw new IllegalArgumentException("load(collection) : collection is null");
        }
        
//        //--- Initialize the DataSet ( prepare to load )
//        // initBeforeLoad();
//        dataset.initBeforeLoad();

        //--- Init the rows
        for ( Object rowValues[] : collection )
        {
//            if ( oRow instanceof Object[] )
//            {
//                Object[] values = (Object[]) oRow ;
//                DataRow dr = new DataRow( values.length, values);
//                dataset.addRow(dr);
//            }
//            else {
//                throw new TelosysException("Invalid row type (not Object[]) "  );
//            }
            DataRow dr = new DataRow( rowValues.length, rowValues);
            dataset.addRow(dr);
        }
        
        //--- Return the number of rows in the DataSet
        //return getRowCount();
        return dataset.getRowCount();
    }

    public DataSet load( Object[][] rows) 
    {
    	DataSet dataset = new DataSet();
    	load(dataset, rows);
    	return dataset ;
    }

    /**
     * Loads the values of the given array in the given DataSet 
     * @param dataset - an exiting DataSet where the rows will be added
     * @param rows
     * @return the number of rows in the DataSet
     */
    public int load(DataSet dataset, Object[][] rows) 
    {
        if ( rows == null )
        {
            throw new IllegalArgumentException("load(rows) : rows is null");
        }
        
//        //--- Initialize the DataSet ( prepare to load )
//        dataset.initBeforeLoad();

        //--- Init the rows
        for ( Object[] rowValues : rows )
        {
            DataRow dr = new DataRow( rowValues.length, rowValues);
            dataset.addRow(dr);
        }
        
        //--- Return the number of rows in the DataSet
        return dataset.getRowCount();
    }

    //-----------------------------------------------------------------------------
    //-----------------------------------------------------------------------------
}